<template>
  <div>
    <h5 class="text-muted">当前任务：项目调查</h5>
    <el-form class="query-table-form" @submit.native.prevent :inline="true" :model="params">
      <el-form-item>
        <el-input
          v-model="params.customer_name"
          placeholder="企业名称查询"
          :clearable="true"
          @change="getData"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="params.lender_idcard"
          placeholder="身份证号码查询"
          :clearable="true"
          @change="getData"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="params.contact_person"
          placeholder="经营者名称查询"
          :clearable="true"
          @change="getData"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" @click="getData()">查询</el-button>
      </el-form-item>
    </el-form>

    <c-table border class="table-sm" v-loading="loading" :columns="tableColumns" :data="tableData">
      <template v-slot:address="scope">
        <div class="text-truncate" :title="scope.row.address">{{ scope.row.address }}</div>
      </template>

      <template v-slot:status="scope">
        <div
          class="badge"
          :title="scope.row.bank_approval_des || ''"
          :class="`badge-${scope.row.statusStyle}`"
        >
          {{ scope.row.statusLabel }}
        </div>
      </template>
    </c-table>

    <div class="query-pagination">
      <div></div>
      <el-pagination
        layout="total, prev, pager, next, jumper"
        :current-page.sync="params.index"
        :page-size="params.size"
        :total="tableCount"
        @current-change="getData"
      ></el-pagination>
    </div>

    <el-dialog
      :visible.sync="reportVisible"
      :close-on-click-modal="false"
      append-to-body
      top="30px"
      width="80%"
    >
      <credit-file
        :view-mode="viewMode"
        :query-id="queryId"
        @success="handleFileSuccess"
      ></credit-file>
    </el-dialog>
  </div>
</template>
<script>
import CreditFile from '@/credit/components/creditFile/Index'
import { mapGetters } from 'vuex'
import { enumOrgClass, enumFarmType } from '@/credit/plugins/enum'
import { enumBankApproval } from '@/gov/plugins/enum'
const enumStatusStyle = {
  0: 'info',
  1: 'warning',
  2: 'success',
  3: 'danger',
}

export default {
  components: {
    CreditFile,
  },
  props: {},
  data() {
    return {
      params: {
        index: 1,
        size: 10,
        lender_idcard: '',
        customer_name: '',
        contact_person: '',
        'is_approval.eq': 1,
        'submit_type.eq': 4,
        'bank_code.endlike': '',
      },
      loading: false,

      tableName: 'customer_related_view',
      tableData: [],
      tableCount: 0,
      tableColumns: [
        {
          attrs: { label: '主体名称', prop: 'customer_name', minWidth: '120px' },
          process: (v, row) => ({
            attrs: { class: 'project-history', attrs: { 'data-history': row.is_history } },
          }),
        },
        { attrs: { label: '地址', prop: 'address' }, slot: 'address' },
        {
          attrs: { label: '组织形式', prop: 'org_class', align: 'center' },
          process: v => enumOrgClass[v] || '-',
        },
        {
          attrs: { label: '企业类型', prop: 'farm_type', align: 'center' },
          process: v => enumFarmType[v] || '-',
        },
        { attrs: { label: '经营者姓名', prop: 'contact_person', align: 'center' } },
        { attrs: { label: '联系电话', prop: 'mobile_no', align: 'center' } },

        { attrs: { label: '预审金额', prop: 'approved_funds', align: 'center' } },
        { attrs: { label: '项目状态', prop: 'statusLabel', align: 'center' }, slot: 'status' },
        {
          attrs: { label: '操作', align: 'center' },
          operations: [
            {
              content: '调查',
              icon: 'fa fa-edit',
              event: this.handleEdit,
              ifRender: row => {
                return +row.bank_census === 0
              },
            },
            { content: '数字信用档案', icon: 'fa fa-file-text', event: this.handleView },
          ],
        },
      ],

      // 信用档案
      queryId: '',
      reportVisible: false,
      viewMode: false,
    }
  },
  computed: {
    ...mapGetters(['user', 'userId', 'clientId', 'job']),
  },
  watch: {
    $route(route) {
      if (route.name === 'gxsPrelim') {
        this.getData()
      }
    },
  },
  created() {
    this.params['bank_code.endlike'] = this.user.orgCode
    this.getData()
  },
  mounted() {},
  methods: {
    async getData() {
      const params = { ...this.params }
      const status = this.$route.query.status
      if (status !== undefined) {
        params['bank_census.eq'] = status
      }

      this.loading = true
      const [res] = await this.$utils.promise(
        this.$api.getData(this.$serviceName, this.tableName, params)
      )
      this.loading = false

      const data = (res && res.data) || {}
      this.tableCount = data.totalCount
      this.tableData = (data.data || []).map(item => {
        item.statusLabel = enumBankApproval[item.bank_census] || '-'
        item.statusStyle = enumStatusStyle[item.bank_census] || ''
        return item
      })
    },
    handleEdit(row) {
      this.viewMode = false
      this.queryId = row.customer_related_id
      this.reportVisible = true
    },
    handleView(row) {
      this.viewMode = true
      this.queryId = row.customer_related_id
      this.reportVisible = true
    },
    handleFileSuccess() {
      this.$store.dispatch('getTaskCountGxs')
      this.getData()
    },
  },
}
</script>
<style lang="scss" scoped></style>
